package com.bs.service.impl;

import java.util.*;
import com.bs.bean.Admin;
import com.bs.service.AdminService;
import com.bs.mapper.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service
/*业务层：AdminService接口的实现类，调用AdminDao接口的实现类对数据进行相关业务等操作*/
public class AdminServiceImpl implements AdminService{

//	注入AdminDao接口的实现类对象
	@Autowired
	AdminMapper adminDao;
//	登录校验
	public Admin login(String username, String password) {
		List<Admin> list = findAll(new Admin().setUsername(username).setPassword(password));
		if (list.size()>0) {
			return list.get(0);
		}else {
			throw new RuntimeException("用户名和密码不匹配");
		}
	}
//	根据条件查询admin中的数据
	public List<Admin> findAll(Admin query) {
		// TODO Auto-generated method stub
		return findAll(query==null?new Admin():query,true);
	}
	
//	根据条件和模式查询admin中的数据
	public List<Admin> findAll(Admin whereWrap, boolean mode) {
		// TODO Auto-generated method stub
		List<Admin> list = adminDao.selectByExample(whereWrap);
		if (whereWrap!=null) {
			Iterator<Admin> iterator = list.iterator();
			while (iterator.hasNext()) {
				Admin admin = (Admin) iterator.next();
				if (whereWrap.getUsername() != null&&(!(whereWrap.getUsername()+"").equals(""))) {
					if (mode) {
						if (!(admin.getUsername() + "").equals(whereWrap.getUsername() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(admin.getUsername() + "").contains(whereWrap.getUsername() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getPassword() != null&&(!(whereWrap.getPassword()+"").equals(""))) {
					if (mode) {
						if (!(admin.getPassword() + "").equals(whereWrap.getPassword() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(admin.getPassword() + "").contains(whereWrap.getPassword() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
			} 
		}
		return list;
	}
	
//	查询admin中的数据
	public List<Admin> findAll() {
		// TODO Auto-generated method stub
		return findAll(new Admin());
	}
	
//	根据主键查询admin中的一条数据
	public Admin findById(String username) {
		// TODO Auto-generated method stub
		return adminDao.selectByPrimaryKey(username);
	}

//	选择性添加admin表中的admin数据
	public int insert(Admin admin) {
		// TODO Auto-generated method stub
		return adminDao.insertSelective(admin);
	}

//	根据主键更新admin表中的一条记录
	public int updateById(Admin admin) {
		// TODO Auto-generated method stub
		return adminDao.updateByPrimaryKey(admin);
	}

//	根据主键选择性更新admin表中的admin数据
	public int updateByIdSelective(Admin admin) {
		// TODO Auto-generated method stub
		Admin dbAdmin = findById(admin.getUsername());
		if (admin.getUsername()==null) {
			admin.setUsername(dbAdmin.getUsername());
		}
		if (admin.getPassword()==null) {
			admin.setPassword(dbAdmin.getPassword());
		}
		return updateById(admin);
	}

//	根据条件删除admin表中的数据
	public int delete(Admin whereWrap) {
		// TODO Auto-generated method stub
		return delete(whereWrap,true);
	}

//	根据条件和模式删除admin表中的数据
	public int delete(Admin whereWrap, boolean mode) {
		// TODO Auto-generated method stub
		List<Admin> list = findAll(whereWrap, mode);
		for (Admin admin : list) {
			deleteById(admin.getUsername());
		}
		return list.size();
	}
//	根据主键删除admin表中的一条记录
	public int deleteById(String username) {
		// TODO Auto-generated method stub
		return adminDao.deleteByPrimaryKey(username);
	}

//	根据主键集合批量删除admin表中的记录
	public int deleteByIds(List<String> list){
		for (String username : list) {
			deleteById(username);
		}
		return list.size();
	}
}
